package com.pokeoffice.builder;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.pokeoffice.users.Admin;
import com.pokeoffice.users.Nurse;
import com.pokeoffice.users.Professor;
import com.pokeoffice.users.Trainer;
import com.pokeoffice.users.User;
import com.pokeoffice.users.UserType;


/**
 * The Class UserBuilder.
 */
public class UserBuilder {

	/**
	 * Builds the from result set.
	 *
	 * @param rs the rs
	 * @return the user
	 */
	public static User buildFromResultSet(ResultSet rs) {
		try {
			if (rs.next()) {
				System.out.println("Building User");
				User use = null; 
				
				if (UserType.TRAINER.toString().equalsIgnoreCase(rs.getString("access"))) {
					use = new Trainer();
					use.setUserType(UserType.TRAINER);
				} else if (UserType.NURSE.toString().equalsIgnoreCase(rs.getString("access"))) {
					use = new Nurse();
					use.setUserType(UserType.NURSE);
				} else if (UserType.PROFESSOR.toString().equalsIgnoreCase(rs.getString("access"))) {
					use = new Professor();
					use.setUserType(UserType.PROFESSOR);
				} else if (UserType.ADMIN.toString().equalsIgnoreCase(rs.getString("access"))) {
					use = new Admin();
					use.setUserType(UserType.ADMIN);
				} else {
					return null;
				}
			
				use.setBirthdate(rs.getString(5));
				use.setCity(rs.getString(8));
				use.setGender(rs.getString(6));
				use.setID(rs.getLong(1));
				use.setInsurance(rs.getString(11));
				use.setName(rs.getString(4));
				use.setPharmacy(rs.getString(10));
				use.setPhone(rs.getString(7));
				use.setRegion(rs.getString(9));
				
				return use;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	/**
	 * Builds the from string array.
	 *
	 * @param s the s
	 * @return the user
	 */
	public static User buildFromStringArray(String[] s) {
		User use = null;
		
		if (UserType.TRAINER.toString().equalsIgnoreCase(s[0])) {
			use = new Trainer();
			use.setUserType(UserType.TRAINER);
		} else if (UserType.NURSE.toString().equalsIgnoreCase(s[0])) {
			use = new Nurse();
			use.setUserType(UserType.NURSE);
		} else if (UserType.PROFESSOR.toString().equalsIgnoreCase(s[0])) {
			use = new Professor();
			use.setUserType(UserType.PROFESSOR);
		} else if (UserType.ADMIN.toString().equalsIgnoreCase(s[0])) {
			use = new Admin();
			use.setUserType(UserType.ADMIN);
		} else {
			return null;
		}
		
		use.setBirthdate(s[5] + "-" + s[6] + "-" + s[7]);
		use.setCity(s[3]);
		use.setGender(s[2]);
		use.setInsurance(s[8]);
		use.setName(s[1]);
		use.setPharmacy(s[9]);
		use.setPhone(s[10]);
		use.setRegion(s[4]);
		return use;
	}
}
